﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using Microsoft.AspNet.SignalR;
namespace signalR消息推送机制.SignalR
{

    public class PersistentChat : PersistentConnection
    {
        static List<string> connectionList;
        static PersistentChat()
        {
            connectionList = new List<string>();//连接池
        }

        protected override Task OnReceived(IRequest request, string connectionId, string data)
        {
            // Broadcast data to all clients
            data = string.Format("链接ＩＤ:{0},数据是:{1} 时间是:{2}", connectionId, data, DateTime.Now.ToString());
            return Connection.Send(connectionId, data);
        }

        protected override Task OnConnected(IRequest request, string connectionId)
        {
            connectionList.Add(connectionId);//从链接列表中添加
            base.OnConnected(request, connectionId);
            return Connection.Send(connectionId, "连接成功,当前接口ID" + connectionId);
        }

        protected override Task OnDisconnected(IRequest request, string connectionId)
        {
            connectionList.Remove(connectionId);//从链接列表中移除
            base.OnDisconnected(request, connectionId);
            return Connection.Send(connectionId, "断开成功,当前接口ID" + connectionId);
        }

        protected override Task OnReconnected(IRequest request, string connectionId)
        {
            return base.OnReconnected(request, connectionId);
        }
    }

}